/**
 * 
 */
package edu.bucknell.edash;

import java.io.IOException;
import java.util.Scanner;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;


/**
 * @author abb011
 *
 */
public class PrimaryScreen extends Fragment {

	
	public Gauge mainVoltage;
	public Gauge chargeRemoved;
	public Gauge mainCurrent;
	public TextView ambientTemp;
	public TextView batteryTemp;
	public TextView chargeRemovedText;
	public TextView auxValues;
	public ToggleButton loggingButton;
	public EdashMain main;
	public ImageView warning;
	public static Fragment newInstance(Context context) {
		PrimaryScreen f = new PrimaryScreen();	
		
		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		
		// Selecting Which XML to use for the fragment
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.primary_screen, null);
		
		//Getting the main activity
		main = (EdashMain) getActivity();
		
		// Getting fields From XML
		ambientTemp = (TextView)root.findViewById(R.id.ambientTemp);
		batteryTemp = (TextView)root.findViewById(R.id.batteryTemp);
		chargeRemovedText= (TextView)root.findViewById(R.id.chargeRemovedText);
		warning =(ImageView)root.findViewById(R.id.warning);
		
		//Creating a button listener for the log button after reading it from the xml
		loggingButton = (ToggleButton)root.findViewById(R.id.loggingButton);
		loggingButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					main.createFile();
				}else{
					try {
						main.outputFile.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
		});
		
		// Loading more elements from XML
		mainVoltage = (Gauge) root.findViewById(R.id.mainBatVoltage);
		chargeRemoved = (Gauge) root.findViewById(R.id.chargeRemovedGauge);
		chargeRemoved.setOnClickListener(new View.OnClickListener(){
			@Override
			/**
			 * Setting the charge removed/remaning guage to be clickable with a pop up that queries for the 
			 * The current charge remaining
			 */
			public void onClick(View v) {
				final EditText textField = new EditText(getActivity());
				DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
				    @Override
				    public void onClick(DialogInterface dialog, int which) {
				    	EdashMain main = (EdashMain)getActivity();
				        switch (which){
				        case DialogInterface.BUTTON_POSITIVE:
				            //Set Charge Removed button clicked
				        	String text = textField.getText().toString();
				        	Scanner parser = new Scanner(text);
				        	// Parsing input
				        	if(parser.hasNextFloat()){
				        		System.out.println("Has a Float");
				        		float chargeRemaining = parser.nextFloat();
				        		System.out.println(chargeRemaining);
				        		float chargeTaken = main.gChargeRemoved.getMaxVal()-chargeRemaining;
				        		main.chargeRemoved = chargeTaken;
				        		main.gChargeRemoved.setTargetVal(chargeTaken);
				        	}else{
				        		System.out.println("No new float");
				        		System.out.println(text);
				        	}
				      
				            break;

				        case DialogInterface.BUTTON_NEGATIVE:
				            //Cancel button clicked
				        	// do nothing
				            break;
				        }
				    }
				};
				AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
				builder.setView(textField);
				builder.setMessage("Enter The Current Charge Remaining").setPositiveButton("Set Charge Remaining", dialogClickListener)
				    .setNegativeButton("Cancel", dialogClickListener).show();
				
				System.out.println("Clicked!!!!!");
				
				}
			});
		mainCurrent = (Gauge) root.findViewById(R.id.mainBatCurrent);

		// Connecting GUI Items to the main activity
		main.gMainCurrent = mainCurrent;
		main.gChargeRemoved = chargeRemoved;
		main.gMainVoltage = mainVoltage;
		main.tAmbientTemp = ambientTemp;
		main.tBatteryTemp = batteryTemp;
		main.tChargeRemaining = chargeRemovedText;
		main.logButton=loggingButton;
		main.iWarning = warning;
		
	
		
		//Return the Frame
		return root;
	}
}
